/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ECH
 */
public class SessionUtils {

    public static String SESS_USERID = "uID";
    public static String SESS_ALBUMID = "aID";

    public static String getUserID(HttpSession session) {
        String userID = null;
        Object attribute = session.getAttribute(SESS_USERID);
        if (attribute != null && attribute instanceof String) {
            userID = (String) attribute;
        }
        return userID;
    }

    public static boolean setUserID(String userID, HttpSession session) {
        boolean success = false;
        try {
            session.setAttribute(SESS_USERID, userID);
            success = true;
            //CX: 8 hrs - 28800
            //CX: 1 day - 86499
            //CX: 30 days - 2592000
            session.setMaxInactiveInterval(86499);
        } catch (Exception ex) {
            //TODO: Log4j
            ex.printStackTrace();
        }
        return success;
    }
    
    public static boolean setUserID(String userID, HttpSession session, boolean remember) {
        boolean success = false;
        try {
            session.setAttribute(SESS_USERID, userID);
            if (remember) {
                //CX: 30 days
                session.setMaxInactiveInterval(2592000);
            }
            success = true;
        } 
        catch (Exception ex) {
            //TODO: Log4j
            ex.printStackTrace();
        }
        return success;
    }

    public static void removeUserID(HttpSession session) {
        session.removeAttribute(SESS_USERID);
    }
    
}
